package spring.superrun.record.mvc.backendcomputecenter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import spring.superrun.record.mvc.di.SpringUtil;
import spring.superrun.record.mvc.entity.SuperrunLocations;
import spring.superrun.record.mvc.entity.SuperrunRecordranking;
import spring.superrun.record.mvc.entity.SuperrunRunrecords;
import spring.superrun.record.mvc.entity.SuperrunUserinfos;
import spring.superrun.record.mvc.service.SuperrunLocationsService;
import spring.superrun.record.mvc.service.SuperrunRecordrankingService;
import spring.superrun.record.mvc.service.SuperrunRunrecordsService;
import spring.superrun.record.mvc.service.SuperrunUserinfosService;
import spring.superrun.record.mvc.setup.RedisConfig;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;

/**
 * 构造消费者业务逻辑
 */
public class RedisListingConsumer{
    private static Jedis jedis =SpringUtil.getBean("redisClient");
    private SuperrunRecordrankingService superrunRecordrankingService
            =SpringUtil.getBean("superrunRecordrankingService");
    private SuperrunRunrecordsService superrunRunrecordsService
            =SpringUtil.getBean("superrunRunrecordsService");
    private SuperrunUserinfosService superrunUserinfosService
        =SpringUtil.getBean("superrunUserinfosService");
    private SuperrunLocationsService superrunLocationsService
            =SpringUtil.getBean("superrunLocationsService");

    public void run() {
            //消费者消费一个id
            //非 ---阻塞式brpop，List中无数据时阻塞，参数0表示一直阻塞下去，直到List出现数据
            String recordId = jedis.rpop(RedisConfig.RecordRankingQueueKey);
            if (null==recordId||recordId.isEmpty())
            {
                // 强烈建议无论如何休眠一段时间，防止突发大流量导致webApp进程CPU满载
                // ，自行根据场景设置合理休眠时间
                return;
            }
            SuperrunRunrecords userAndRunRecords = superrunRunrecordsService.queryById(recordId);
            SuperrunRecordranking rankingIsExist =
                    superrunRecordrankingService.queryByUserId(userAndRunRecords.getUserid());
            if (rankingIsExist ==null)
            {
                rankingIsExist = new SuperrunRecordranking();
                rankingIsExist.Add();
                rankingIsExist.setUserid(userAndRunRecords.getUserid());;
            }
            List<SuperrunRunrecords> lastRecords = superrunUserinfosService
                    .queryByIdHaveRecords(userAndRunRecords.getUserid()).getRecords();
            //按时间做降序排序
            lastRecords.sort((q1,q2)->q2.getFinishtime().compareTo(q1.getFinishtime()));

            rankingIsExist.setLocationid(lastRecords.get(0).getLocationid());
            SuperrunLocations location = superrunLocationsService.queryById(rankingIsExist.getLocationid());

            rankingIsExist.setLocationname(location.getLocationname());
            rankingIsExist.setLocationorderid(location.getOrderid());

            double totalDistance = lastRecords.stream()
                    .max(Comparator.comparingDouble(SuperrunRunrecords::getFromstartdistance))
                    .get().getFromstartdistance();

            rankingIsExist.setLocationcount(lastRecords.size());

            rankingIsExist.setTotalseconds(lastRecords.stream()
                    .mapToInt(SuperrunRunrecords::getFromlasttimesecond).sum());
            double avgSpeed =totalDistance<=0?0.0: (totalDistance / rankingIsExist.getTotalseconds());
            rankingIsExist.setRanking(0);
            rankingIsExist.setAvgspeed(avgSpeed);//每秒跑多少米

            double maxAvgSpeed = lastRecords.stream()
                    .max(Comparator.comparingDouble(SuperrunRunrecords::getAvgspeed))
                    .get().getAvgspeed();

            rankingIsExist.setMaxavgspeed(maxAvgSpeed);
            if (superrunRecordrankingService.AddOrUpdate(rankingIsExist))
            {
                return;
            }
             System.out.println("计划任务执行中...");
            return;
            //Log.Information($"姓名:{userAndRunRecords.ChineseName}且用户ID：{userAndRunRecords.Id}的跑步打卡记录统计保存成功!!");

    }
}
